﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database;
using Database.Classes;

public partial class StoredProcedures
{
    public static void UseSupplayUnits(int sId, int pId, BuldingTypes what)
    {
        if (UserDefinedFunctions.FleetOwner(sId, pId) != 0 && UserDefinedFunctions.FleetOwner(sId, pId) != UserDefinedFunctions.PlanetOwner(sId, pId))
            return;

        int owner = UserDefinedFunctions.PlanetOwner(sId, pId).Value;
        int supply = UserDefinedFunctions.GetPlayerInt(owner, "SupplyUnits").Value ;
        if (supply == 0)
            return;
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("", conn))
            {
                switch (what)
                {
                    case BuldingTypes.Farm:
                        cmd.CommandText = "Update planets Set Farms = Farms + 1 Where PlanetId = @pId and SystemId = @sId";
                        break;
                    case BuldingTypes.Factory:
                        cmd.CommandText = "Update planets Set Factories = Factories + 1 Where PlanetId = @pId and SystemId = @sId";
                        break;
                    case BuldingTypes.Cybernet:
                        cmd.CommandText = "Update planets Set Cybernets = Cybernets + 1 Where PlanetId = @pId and SystemId = @sId";
                        break;
                    case BuldingTypes.Lab:
                        cmd.CommandText = "Update planets Set Labs = Labs + 1 Where PlanetId = @pId and SystemId = @sId";
                        break;
                }
                cmd.Parameters.Add("@sId", SqlDbType.Int).Value = sId;
                cmd.Parameters.Add("@pId", SqlDbType.Int).Value = pId;
                cmd.ExecuteNonQuery();
                cmd.CommandText = "Update players set SupplyUnits = SupplyUnits -1 where PlayerID = @p";
                cmd.Parameters.Add("@p", SqlDbType.Int).Value = owner;
                cmd.ExecuteNonQuery();
            }
        }
    }
    public static string Build(int sId, int pId, BuldingTypes what, int quantity)
    {
        if (UserDefinedFunctions.FleetOwner(sId, pId) != 0 && UserDefinedFunctions.FleetOwner(sId, pId) != UserDefinedFunctions.PlanetOwner(sId, pId))
            return string.Empty;
        string s = sId + " " + pId + " " + what.ToString() + " " + quantity.ToString() + "<br/>\n";

        bool market = UserDefinedFunctions.PlayerHasSpecialBulding(UserDefinedFunctions.PlanetOwner(sId, pId).Value, SpecialBuldings.Market);
        if (what == BuldingTypes.Farm || what == BuldingTypes.Factory || what == BuldingTypes.Cybernet || what == BuldingTypes.Lab || what == BuldingTypes.Starbase)
        {
            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("", conn);
                if (cmd.Parameters.Contains("@sId") == false)
                    cmd.Parameters.Add("@sId", SqlDbType.Int).Value = sId;
                if (cmd.Parameters.Contains(@"pId") == false)
                    cmd.Parameters.Add("@pId", SqlDbType.Int).Value = pId;
                cmd.Parameters.Add("@rem", SqlDbType.Float).Value = 0;
                while (quantity > 0)
                {
                    switch (what)
                    {
                        case BuldingTypes.Farm:
                            cmd.CommandText = "Select Farms From Planets Where PlanetId = @pId and SystemId = @sId";
                            break;
                        case BuldingTypes.Factory:
                            cmd.CommandText = "Select Factories From Planets Where PlanetId = @pId and SystemId = @sId";
                            break;
                        case BuldingTypes.Cybernet:
                            cmd.CommandText = "Select Cybernets From Planets Where PlanetId = @pId and SystemId = @sId";
                            break;
                        case BuldingTypes.Lab:
                            cmd.CommandText = "Select Labs From Planets Where PlanetId = @pId and SystemId = @sId";
                            break;
                        case BuldingTypes.Starbase:
                            cmd.CommandText = "Select Starbase From Planets Where PlanetId = @pId and SystemId = @sId";
                            break;
                    }


                    //get current level of building and the remaining pp's to the next level
                    double bulLev = double.Parse(cmd.ExecuteScalar().ToString());
                    cmd.CommandText = "Select ProductionPoints From Planets Where PlanetId = @pId and SystemId = @sId";
                    double pp = double.Parse(cmd.ExecuteScalar().ToString());
                    int ppCost = Equations.BuildingCost((int)bulLev);
                    if (market == true) ppCost = (int)(ppCost * 0.9);
                    double remainingPP = Math.Abs((1.0 - (bulLev - (int)bulLev)) * ppCost * 1.0);
                    s += bulLev.ToString() + "; " + pp.ToString() + " " + ppCost.ToString() + " " + remainingPP.ToString() + "<br/>\n";
                    //update the table
                    if (pp - remainingPP >= 0)
                    {
                        switch (what)
                        {
                            case BuldingTypes.Farm:
                                cmd.CommandText = "Update planets Set Farms = Farms + 1, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Factory:
                                cmd.CommandText = "Update planets Set Factories = Factories + 1, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Cybernet:
                                cmd.CommandText = "Update planets Set Cybernets = Cybernets + 1, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Lab:
                                cmd.CommandText = "Update planets Set Labs = Labs + 1, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Starbase:
                                cmd.CommandText = "Update planets Set Starbase = Starbase + 1, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                        }
                        s += cmd.CommandText + "<br/>";
                        cmd.Parameters["@rem"].Value = remainingPP;
                        cmd.ExecuteNonQuery();
                        // cmd.Parameters.Remove("@rem");
                        quantity--;
                    }
                    else
                        return s;
                }

            }
        }
        else
        {

            int cost = 0;
            int EcoBonus = UserDefinedFunctions.EcoBonus(UserDefinedFunctions.PlanetOwner(sId, pId).Value);
            if (what == BuldingTypes.Cruiser && UserDefinedFunctions.GetPlayerFloat(UserDefinedFunctions.PlanetOwner(sId, pId).Value, "Math").Value < 15)
                return s;
            if (what == BuldingTypes.Battleship && UserDefinedFunctions.GetPlayerFloat(UserDefinedFunctions.PlanetOwner(sId, pId).Value, "Physics").Value < 15)
                return s;
            switch (what)
            {
                case BuldingTypes.CS:
                    cost = int.Parse(GetConfigurationForOption("ColonyShipPrice")) - EcoBonus;
                    break;
                case BuldingTypes.Transport:
                    cost = int.Parse(GetConfigurationForOption("TransportPrice")) - EcoBonus;
                    break;
                case BuldingTypes.Destroyer:
                    cost = int.Parse(GetConfigurationForOption("DestroyerPrice")) - EcoBonus;
                    break;
                case BuldingTypes.Cruiser:
                    cost = 8 * (int.Parse(GetConfigurationForOption("DestroyerPrice")) - EcoBonus);
                    break;
                case BuldingTypes.Battleship:
                    cost = 20 * (int.Parse(GetConfigurationForOption("DestroyerPrice")) - EcoBonus);
                    break;
            }
            cost = cost * quantity;

            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select ProductionPoints From Planets Where PlanetId = @pId and SystemId = @sId", conn);
                try
                {
                    cmd.Parameters.Add("@sId", SqlDbType.Int).Value = sId;
                    cmd.Parameters.Add("@pId", SqlDbType.Int).Value = pId;
                    int productionPP = (int)double.Parse(cmd.ExecuteScalar().ToString());
                    if (productionPP - cost >= 0)
                    {
                        switch (what)
                        {
                            case BuldingTypes.CS:
                                cmd.CommandText = "Update planets Set ColonyShips = ColonyShips + @q, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Transport:
                                cmd.CommandText = "Update planets Set Transports = Transports + @q, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Destroyer:
                                cmd.CommandText = "Update planets Set Destroyers = Destroyers + @q, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Cruiser:
                                cmd.CommandText = "Update planets Set Crusiers = Crusiers + @q, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                            case BuldingTypes.Battleship:
                                cmd.CommandText = "Update planets Set Battleships = Battleships + @q, ProductionPoints = ProductionPoints - @rem Where PlanetId = @pId and SystemId = @sId";
                                break;
                        }
                        cmd.Parameters.Add("@q", SqlDbType.Int).Value = quantity;
                        cmd.Parameters.Add("rem", SqlDbType.Int).Value = cost;
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(cmd.CommandText, ex);
                }
            }
        }
        return s;
    }
};
